package com.example.onlinemallsystem.service.impl;

import com.example.onlinemallsystem.entity.Product;
import com.example.onlinemallsystem.mapper.ProductMapper;
import com.example.onlinemallsystem.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public int insertProduct(Product product) {
        return productMapper.insert(product);
    }

    @Override
    public int deleteProduct(Long id) {
        return productMapper.deleteProduct(id);
    }

    @Override
    public int updateProduct(Product product) {
        return productMapper.update(product);
    }

    @Override
    public Product selectProductById(Long id) {
        return productMapper.selectById(id);
    }

    @Override
    public List<Product> selectAllProducts() {
        return productMapper.selectAll();
    }

    @Override
    public List<Product> selectProductsByCategoryId(Long categoryId) {
        return productMapper.selectByCategoryId(categoryId);
    }

    @Override
    public List<Product> selectProductsByName(String name) {
        return productMapper.selectByName(name);
    }


    @Override
    @Transactional
    public int increaseProductSales(Long id, Integer amount) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("amount", amount);
        return productMapper.increaseSales(params);
    }

    @Override
    public boolean existsByName(String name) {
        int count = productMapper.existsByName(name);
        return count > 0;
    }

        @Override
        public String uploadProductImage(Long productId, MultipartFile file) throws IOException {
            if (file.isEmpty()) {
                throw new IllegalArgumentException("上传文件不能为空");
            }

            // 生成唯一文件名
            String fileName = "product_" + productId + "_" + UUID.randomUUID();

            String uploadDir = "";
            String filePath = uploadDir + "/" + fileName;

            // 保存文件到服务器
            File dest = new File(filePath);
            file.transferTo(dest);

            // 更新商品图片路径
            updateProductImage(productId, "/uploads/" + fileName);

            return "/uploads/" + fileName;
        }

        @Override
        public boolean updateProductImage(Long productId, String imageUrl) {
            Product product = new Product();
            product.setId(productId);
            product.setImage(imageUrl);
            return productMapper.update(product) > 0;
        }

    @Override
    public List<Product> selectProductsBySellerId(Long sellerId) {
        return productMapper.selectProductsBySellerId(sellerId);
    }

    @Override
    public List<Product> selectSellerProductsByName(Long sellerId, String name) {
        return productMapper.selectSellerProductsByName(sellerId, name);
    }

    @Override
    public int updateProductStock(Long id, Integer stock) {
        return productMapper.updateProductStock(id, stock);
    }

    @Override
    public int updateProductPrice(Long id, Double price) {
        return productMapper.updateProductPrice(id, price);
    }
}
